import { Form, Input, InputNumber, Radio, Select } from 'antd';
import { useState } from 'react';
import an58 from '@mmstudio/an000058';
import AddButton from '../../../components/addbutton';
import api from '../../../atoms/api';
import { Message as M1, Result as R1 } from '../../api/professional/course/add.api';
import Textarea from '../../../components/textarea';

export default function Add({ majors, groups }: {
	majors: Pick<ITbmajor, 'majorname' | 'major_id'>[];
	groups: Pick<ITbgroup, 'group_id' | 'groupname'>[];
}) {
	const [d, setd] = useState({} as M1);
	return <>
		<AddButton dlgtitle='新增课程' onSave={async () => {
			if (!d
				|| !d.courseintroduction
				|| !d.coursename
				|| !d.credit
				|| !d.group_id
				|| !d.major_id
				|| !d.semester
				|| !d.type
			) {
				return '缺少必要的参数';
			}
			const res = await an58<R1, M1>(api['/api/professional/course/add'], 'post', d);
			if (res.ok === true) {
				return true;
			}
			return res.message;
		}} >
			<Form.Item required label="专业名称:">
				<Select
					optionFilterProp="children"
					filterOption={(input, option) => {
						return option.children.indexOf(input) >= 0;
					}}
					placeholder='请选择专业名称'
					value={d.major_id}
					onChange={(e) => {
						setd({
							...d,
							major_id: e
						});
					}} >
					{majors.map((it) => {
						return <Select.Option key={it.major_id} value={it.major_id} >{it.majorname}</Select.Option>;
					})}
				</Select>
			</Form.Item>
			<Form.Item required label="课组名称:">
				<Select
					optionFilterProp="children"
					filterOption={(input, option) => {
						return option.children.indexOf(input) >= 0;
					}}
					placeholder='请选择专业名称'
					value={d.group_id}
					onChange={(e) => {
						setd({
							...d,
							group_id: e
						});
					}} >
					{groups.map((it) => {
						return <Select.Option key={it.group_id} value={it.group_id} >{it.groupname}</Select.Option>;
					})}
				</Select>
			</Form.Item>
			<Form.Item required label="课程名称:">
				<Input placeholder='课程名称' value={d.coursename} onChange={(e) => {
					setd({
						...d,
						coursename: e.target.value
					});
				}} />
			</Form.Item>
			<Form.Item required label="课程类型:">
				<Radio.Group onChange={(e) => {
					setd({
						...d,
						type: e.target.value
					});
				}} value={d.type}>
					<Radio value={'必修'}>必修</Radio>
					<Radio value={'选修'}>选修</Radio>
				</Radio.Group>
			</Form.Item>
			<Form.Item required label="课程学分:">
				<InputNumber min={0} placeholder='请输入课程学分' value={d.credit} onChange={(e) => {
					setd({
						...d,
						credit: e
					});
				}} />
			</Form.Item>
			<Form.Item required label="授课学期:">
				<Input placeholder='请输入授课学期' value={d.semester} onChange={(e) => {
					setd({
						...d,
						semester: e.target.value
					});
				}} />
			</Form.Item>
			<Form.Item required label="课程简介">
				<Textarea
					value={d.courseintroduction}
					onChange={(v) => {
						setd({
							...d,
							courseintroduction: v
						});
					}} />
			</Form.Item>
		</AddButton>
	</>;
}
